package com.example.idachuappone.index.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.index.entity.HotServiceItem;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class AdapterHotItem extends BaseAdapter {
	private BitmapUtils bu;
	private Context context;
	private List<HotServiceItem> list;

	private class ViewHolder {
		ImageView img;
		TextView tv_besk;
		TextView tv_desc;
		TextView tv_name;
		TextView tv_price;

		private ViewHolder() {
		}
	}

	public AdapterHotItem(Context context) {
		this.context = context;
		this.bu = BitmapUtilHelper.createBitmapUtil(context);
	}

	public void setList(List<HotServiceItem> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list == null ? 0 : this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (HotServiceItem) this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LinearLayout.inflate(this.context, R.layout.simple_hot_service_item, null);
			holder = new ViewHolder();
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
			holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
			holder.tv_besk = (TextView) convertView.findViewById(R.id.tv_besk);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		HotServiceItem hotServiceItem = this.list.get(position);
		this.bu.display(holder.img, hotServiceItem.getFirst_img(), new BitmapLoadCallBack<View>() {
			@Override
			public void onLoadCompleted(View view, String arg1, Bitmap bitmap, BitmapDisplayConfig arg3,
					BitmapLoadFrom arg4) {
				((ImageView) view).setImageBitmap(BitmapUtilHelper.toRoundCorner(bitmap,
						BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY));
				if (bitmap.isRecycled()) {
					bitmap.recycle();
				}
			}

			@Override
			public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
			}
		});
		holder.tv_name.setText(hotServiceItem.getName());
		holder.tv_desc.setText(hotServiceItem.getPeople_num());
		holder.tv_price.setText(hotServiceItem.getPrice());
		holder.tv_besk.setText(hotServiceItem.getBespeak_num() + "\u4eba\u9884\u8ba2");
		return convertView;
	}
}
